package com.sinder.campus_prophet.utils;

import java.util.Random;
import java.util.UUID;

/**
 * 生成唯一值
 * 利用UUID与随机值
 * @author v-xyingde
 * @date 2023/3/20/16:47
 */
public class UniqueCodeUtils {
    public static String uniqueCode() {
        /* 生成UUID，获取随机值 */
        String uuid = UUID.randomUUID().toString().replace("-", "");
        /* 通过随机数，从UUID中随机获取值 */
        Random random = new Random();
        //指定字符串长度，拼接字符并toString
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 8; i++) {
            //获取指定长度的字符串中任意一个字符的索引值
            int number = random.nextInt(uuid.length());
            //根据索引值获取对应的字符
            char charAt = uuid.charAt(number);
            sb.append(charAt);
        }
        return String.valueOf(sb);
    }

    public static void main(String[] args) {
        String str = UniqueCodeUtils.uniqueCode();
        System.out.println(str);
    }
}
